package com.hezhiqin.springshiro.shiroconfig;

import com.hezhiqin.springshiro.shiroconfig.realm.UserRealm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: homeWork
 * @description:
 * @author: hezhiqin
 * @create: 2020-03-08 20:57
 */
@Configuration
public class ShiroConfig {

    /**
    *@Description: 创建ShiroFilterFactoryBean
    *@Param: 
    *@return: 
    *@Author: hezhiqin
    *@date: 2020/3/8
    */
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        //设置安全关系
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);
        //设置Shiro过滤器
       /**
       * anon:无需认证（登录）可以访问
       * authc：必须认证可以访问
       * user：如果使用rememberMe可以访问
       * perms：必须得到资源权限才可以访问
       * role ：必须得到角色权限才可以访问
       *
       *
       */
        Map<String,String > filterMap = new HashMap<String,String >();
        filterMap.put("/add","authc");
        filterMap.put("/update","authc");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
        shiroFilterFactoryBean.setLoginUrl("templates/login.html");
        return shiroFilterFactoryBean;
    }

    
    /**
    *@Description: 创建DefaultWebSecurityManager
    *@Param: 
    *@return: 
    *@Author: hezhiqin
    *@date: 2020/3/8
    */
    @Bean
    public DefaultWebSecurityManager getDefaultWebSecurityManager(UserRealm userRealm) {
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager(userRealm);
        return defaultWebSecurityManager;
    }
    
    
    /**
    *@Description: 创建Realm
    *@Param:
    *@return: 
    *@Author: hezhiqin
    *@date: 2020/3/8
    */
    @Bean
    public UserRealm getUserRealm(){
        return new UserRealm();
    }
}
